import { defineStore } from 'pinia'
import { houseInfo } from '@/service/api'

// 用来存放用户信息
export const useUserStore = defineStore('user', {
  state: () => {
    return {
      userInfo: sessionStorage.getItem('user')
        ? JSON.parse(decodeURIComponent(sessionStorage.getItem('user')))
        : {}
    }
  },
  getters: {
    getUserInfo: (state) => {
      return state.userInfo
    },
  },
  actions: {
    setUserInfo(data) {
      sessionStorage.setItem('user', encodeURIComponent(JSON.stringify(data)))
      this.userInfo = data
    }
  }
})

// 用来存放用户信息
export const useBuildingStore = defineStore('building', {
  state: () => {
    return {
      buildingInfo: sessionStorage.getItem('building')
        ? JSON.parse(decodeURIComponent(sessionStorage.getItem('building')))
        : {}
    }
  },
  getters: {
    getUserInfo: (state) => {
      return state.buildingInfo
    },
  },
  actions: {
    async setBuildingInfo(fwbh) {
      try {
        const result = await houseInfo({
          fwbh
        })
        const data = {
          ...result.data,
          region: `${result.data?.xzqdmAlias}-${result.data?.townAlias}-${result?.data?.villageAlias}`
        }
        sessionStorage.setItem('building', encodeURIComponent(JSON.stringify(data)))
        this.buildingInfo = data
      } catch (e) {

      }

    }
  }
})
